package com.dlkj.threadPool.core.httpClient;

import org.apache.http.conn.HttpClientConnectionManager;

import javax.annotation.Resource;

/**
 * @ClassName IdleConnectionEvictor
 * @Author leimingfeng
 * @Date 2019/9/12 15:12
 * @Description 清理无效连接
 * @Version 1.0
 **/
public class IdleConnectionEvictor extends Thread {

    @Resource
    private HttpClientConnectionManager manager;

    private volatile boolean shutdown;

    public IdleConnectionEvictor() {
        super();
        super.start();
    }

    @Override
    public void run() {
        try {
            while (!shutdown) {
                synchronized (this) {
                    wait(5000);
                    // 关闭失效的连接
                    manager.closeExpiredConnections();
                }
            }
        } catch (InterruptedException ex) {
            // 结束
        }
    }

    //关闭清理无效连接的线程
    public void shutdown() {
        shutdown = true;
        synchronized (this) {
            notifyAll();
        }
    }
}
